package com.hutao.medical.controller.audit;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.medical.common.util.Result;
import com.hutao.medical.pojo.borrow.HospitalNote;
import com.hutao.medical.query.AuditQuery;
import com.hutao.medical.query.MedRecordQuery;
import com.hutao.medical.service.BorrowService;
import com.hutao.medical.service.MedRecordAuditService;
import com.hutao.medical.service.MedicalModifyService;

@Controller
@RequestMapping("/audit")
public class MedAuditController {
	private static Logger log = LoggerFactory.getLogger(MedAuditController.class);
	
	@Autowired
	private MedRecordAuditService medRecordAuditService;
	
	@Autowired
	private BorrowService borrowService;
	
	@Autowired
	private MedicalModifyService medicalModifyService;
	
	@RequestMapping("/gotoManualAudit")
	public String gotoManualAudit(){
		return "medaudit/manualreview";
	}
	
	@RequestMapping("/medFileCheck_detail")
	public String medFileCheck_detail(String firstGuid,Model model){
		HospitalNote hospitalNote = borrowService.addHospitalNote(firstGuid);
		model.addAttribute("hospitalNote", hospitalNote);
		return "statics/medFileCheck_detail";
	}
	
	@RequestMapping("/medFileCheck_detail2")
	public String medFileCheck_detail2(String firstGuid,Model model){
		HospitalNote hospitalNote = borrowService.addHospitalNote(firstGuid);
		model.addAttribute("hospitalNote", hospitalNote);
		List<Map<String,Object>> medicalErrorList = medRecordAuditService.getMedicalErrorByFirstGuid(firstGuid);
		model.addAttribute("medicalErrorList", medicalErrorList);
		return "statics/medFileCheck_detail2";
	}
	
	@ResponseBody
	@PostMapping("/addAuditRecordDetail")
	public Result addAuditRecordDetail(@RequestBody AuditQuery auditQuery, 
			HttpServletRequest request, HttpSession session) {
		try {
			
			medRecordAuditService.manualAddMedicalModify(auditQuery);
			return new Result(true,"保存成功!");
		} catch (Exception e) {
			System.out.println(e);
			return new Result(false, "系统异常!");
		}
		
	}
	
	@ResponseBody
	@PostMapping("/delMedicalError")
	public Result delMedicalError(Integer medicalModifyId){
		Result result = null;
		try {
			medRecordAuditService.deleteMedicalModify(medicalModifyId);
			result = new Result(true);
		} catch (Exception e) {
			result = new Result(false);
		}
		return result;
	}
	
	
	/**
	 * 自动审核
	 * @param auditQuery
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/autoAudit")
	public Result autoAudit(MedRecordQuery medRecordQuery,Model model){
		Result result = null;
		try {
			//从核心里面获取已归档的病案----->firstGuid
			List<String> firstGuidList = medRecordAuditService.getFirstGuidList(medRecordQuery);
			//
			System.out.println(firstGuidList);
			medRecordAuditService.getAutoAuditList(firstGuidList);
			//查询审核----->未修改的病案
			//auditQuery.setStatus(1);
			Map<String,Object> map = medRecordAuditService.getMedicalAuditErrorList(medRecordQuery);
			result = new Result(true,"查询成功!",map);
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result(false,"查询失败!");
		}
		return result;
	}
	
	
	
	
	
	
	
	@GetMapping("/getMedicalAuditByFirstGuid")
	@ResponseBody
	public Result getMedicalAuditByFirstGuid(String firstGuid) {
		try {
			List<Map<String, Object>> list = medicalModifyService.getMedicalAuditByFirstGuid(firstGuid);
			return new Result(true, "查询成功", list);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("----病案身材查询异常----" + e);
			return new Result(false, "系统异常!");
		}
			
	}
	
	
	
	
	
	
	
}
